package com.huluobo.a2207mmonthdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.navigation.NavigationBarView;
import com.huluobo.a2207mmonthdemo.adapter.CommonPagerAdapter;
import com.huluobo.a2207mmonthdemo.databinding.ActivityMainBinding;
import com.huluobo.a2207mmonthdemo.home.HomeFragment;
import com.huluobo.a2207mmonthdemo.message.MessageFragment;
import com.huluobo.a2207mmonthdemo.mine.MineFragment;
import com.huluobo.a2207mmonthdemo.startLive.StartLiveActivity;
import com.huluobo.a2207mmonthdemo.vidoes.VideosFragment;
import com.huluobo.base.mvvm.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private CommonPagerAdapter commonPagerAdapter;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void initData() {
        fragments.clear();
        fragments.add(new HomeFragment());
        fragments.add(new VideosFragment());
        fragments.add(new MessageFragment());
        fragments.add(new MineFragment());

        commonPagerAdapter = new CommonPagerAdapter(this, fragments);
        binding.mainVp2.setAdapter(commonPagerAdapter);
    }

    @Override
    protected void initView() {

        binding.mainVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        binding.mainBottomNav.getMenu().getItem(0).setChecked(true);
                        break;
                    case 1:
                        binding.mainBottomNav.getMenu().getItem(1).setChecked(true);
                        break;
                    case 2:
                        binding.mainBottomNav.getMenu().getItem(3).setChecked(true);
                        break;
                    case 3:
                        binding.mainBottomNav.getMenu().getItem(4).setChecked(true);
                        break;
                }
            }
        });

        binding.mainBottomNav.setOnItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.main_nav_home:
                    binding.mainVp2.setCurrentItem(0);
                    break;
                case R.id.main_nav_videos:
                    binding.mainVp2.setCurrentItem(1);
                    break;
                case R.id.main_nav_start_live:
                    startActivity(new Intent(this, StartLiveActivity.class));
                    break;
                case R.id.main_nav_message:
                    binding.mainVp2.setCurrentItem(2);
                    break;
                case R.id.main_nav_mine:
                    binding.mainVp2.setCurrentItem(3);
                    break;
            }
            return true;
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        binding.mainVp2.setCurrentItem(1);
        binding.mainBottomNav.getMenu().getItem(1).setChecked(true);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
}